package com.moons.uvcdemo.eth;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import static com.moons.uvcdemo.eth.TEthernet.TAG;

public class EthernetTest {
	protected Handler mHandler = null;
	protected Context mContext = null;

	protected TEthernetBase mTEthernet = null;
	private boolean mReceiverTag = false; // 广播注册标记

	public EthernetTest(Context context, Handler handler) {
		mContext = context;
		mHandler = handler;
		mTEthernet = new TEthernet(mContext);
	}

	public void getStart() {

	}

	public void goStop() {

	}
	public void updateTestResult(final int msgType) {
//		if (msgType == MessageType.Ethernet.ETHERNET_NORMAL_INFO) {
//			Log.e("TAG", "ip: " + mTEthernet.getIpAddress("eth0"));
//			Log.e("", "netMask: " + mTEthernet.getNetMask("eth0"));
//			Log.e("", "gate way: " + mTEthernet.getGateWay());
//			Log.e("", "dns1: " + mTEthernet.getDns1(mContext));
//			Log.e("", "mac: " + mTEthernet.getMac());
//
//			if (null == mHandler) {
//				Log.e(TAG,"handler is null, please set handler");
//				return;
//			}
//
//			final Bundle bundle = new Bundle();
//			bundle.putString(MessageType.Ethernet.IP, mTEthernet.getIpAddress("eth0"));
//			bundle.putString(MessageType.Ethernet.NETMASK,
//					mTEthernet.getNetMask("eth0"));
//			bundle.putString(MessageType.Ethernet.GATEWAY,
//					mTEthernet.getGateWay());
//			bundle.putString(MessageType.Ethernet.DNS_1, mTEthernet.getDns1(mContext));
//			bundle.putString(MessageType.Ethernet.MAC, mTEthernet.getMac());
//			bundle.putBoolean(MessageType.Ethernet.QUALIFIED_MAC,
//					mTEthernet.isMacQualified());
//			Message msg = mHandler
//					.obtainMessage(MessageType.Ethernet.ETHERNET_NORMAL_INFO);
//			mHandler.removeMessages(msg.what);
//			msg.setData(bundle);
//			mHandler.sendMessage(msg);
//		}
	}
	
	public static  boolean isNetworkOnline() {
        Runtime runtime = Runtime.getRuntime();
        try {
            Process ipProcess = runtime.exec("ping -c 3 114.114.114.114");
            int exitValue = ipProcess.waitFor();
            return (exitValue == 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
	}

	protected boolean canConnection(String hostName, int timeOut) {
		BufferedReader reader = null;
		InputStreamReader is = null;
		try {
			InetAddress inet = InetAddress.getByName(hostName);
			// Log.d(TAG,"inet: " + inet.getHostAddress() + ", is ipv4formate: " +
			// isIpv4Format(inet.getHostAddress()));
			if (null != inet && isIpv4Format(inet.getHostAddress())) {
				URL url = new URL("http://" + hostName);
				HttpURLConnection conn = (HttpURLConnection) url
						.openConnection();
				conn.setConnectTimeout(timeOut);
				conn.setDoOutput(true);
				conn.setDoInput(true);
				conn.setUseCaches(false);
				conn.setRequestProperty("Charsert", "UTF-8");
				conn.setInstanceFollowRedirects(true);
				conn.setRequestMethod("POST");
				conn.setRequestProperty("Content-Type",
						"application/x-www-form-urlencoded");
				conn.connect();
				is = new InputStreamReader(conn.getInputStream());
				reader = new BufferedReader(is);
				conn.disconnect();
				return true;
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (reader != null) {
					reader.close();
					reader = null;
				}
				if (is != null) {
					is.close();
					is = null;
				}
			} catch (Exception ex) {
				ex.printStackTrace();
				return false;
			}
		}
	}

	private boolean isIpv4Format(final String ip) {
		String[] fields = ip.replace(" ", "").split("[.]");
		if (4 != fields.length) {
			return false;
		}

		String field = null;
		for (int i = 0; i < fields.length; i++) {
			field = fields[i];
			if (field.matches("[0-9]{0,3}") && Integer.parseInt(field) < 255) {
				continue;
			} else {
				return false;
			}
		}
		return true;
	}
}